package com.karaku.karaku_collector;

import android.content.Context;
import android.content.SharedPreferences;

import com.karaku.karaku_collector.util.CameraUtil;
import com.karaku.karaku_collector.util.Size;

import io.agora.rtc.Constants;

/**
 * Created by guoping on 2017/10/27.
 */

public class LocalSetting {

    public static final int DIR_FRONT = 1;
    public static final int DIR_LATERAL = 2;

    private static LocalSetting instance;

    public static LocalSetting getInstance() {
        if (instance == null) {
            instance = new LocalSetting();
        }
        return instance;
    }

    private Boolean started;
    private String roomId;
    private Integer direction;
    private Integer videoProfile;
    private Float scale;
    private Size previewSize;

    private Float cusFrameHorizontalBorderRatio;
    private Float cusFrameVerticalBorderRatio;
    private float[] whiteBalance;

    SharedPreferences spf = App.getInstance().getSharedPreferences("setting", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor;

    private LocalSetting() {
        editor = spf.edit();
    }

    public boolean isStarted() {
        if (started == null) {
            started = spf.getBoolean("started", false);
        }
        return started;
    }

    public void setStarted(Boolean started) {
        this.started = started;
        editor.putBoolean("started", started);
        editor.apply();
    }

    public String getRoomId() {
        if (roomId == null) {
            roomId = spf.getString("roomId", null);
        }
        return roomId;
    }

    public void setRoomId(String roomId) {
        this.roomId = roomId;
        editor.putString("roomId", roomId);
        editor.apply();
    }

    public int getDirection() {
        if (direction == null) {
            direction = spf.getInt("direction", 1);
        }
        return direction;
    }

    public void setDirection(Integer direction) {
        this.direction = direction;
        editor.putInt("direction", direction);
        editor.apply();
    }

    public Integer getVideoProfile() {
//        return Constants.VIDEO_PROFILE_480P;
        if (videoProfile == null) {
            videoProfile = spf.getInt("videoProfile", Constants.VIDEO_PROFILE_480P);
        }
        return videoProfile;
    }

    public void setVideoProfile(Integer videoProfile) {
        this.videoProfile = videoProfile;
        editor.putInt("videoProfile", videoProfile);
        editor.apply();
    }

    public float getScale() {
        if (scale == null) {
            scale = spf.getFloat("scale", 0.f);
        }
        return scale;
    }

    public void setScale(float scale) {
        this.scale = scale;
        editor.putFloat("scale", scale);
        editor.apply();
    }

    public Size getPreviewSize() {
//        return CameraUtil.getBestCameraSize();
        Size s = CameraUtil.getBestCameraSize();
        if (previewSize == null) {
            int w = spf.getInt("preview_width", s.width);
            int h = spf.getInt("preview_height", s.height);
            previewSize = new Size(w, h);
        }
        return previewSize;
    }

    public void setPreviewSize(Size previewSize) {
        this.previewSize = previewSize;
        editor.putInt("preview_width", previewSize.width);
        editor.putInt("preview_height", previewSize.height);
        editor.apply();
    }

    public float getCusFrameHorizontalBorderRatio() {
        if (cusFrameHorizontalBorderRatio == null) {
            cusFrameHorizontalBorderRatio = spf.getFloat("cusFrameHorizontalBorderRatio", 0.f);
        }
        return cusFrameHorizontalBorderRatio;
    }

    public void setCusFrameHorizontalBorderRatio(Float cusFrameHorizontalBorderRatio) {
        this.cusFrameHorizontalBorderRatio = cusFrameHorizontalBorderRatio;
        editor.putFloat("cusFrameHorizontalBorderRatio", cusFrameHorizontalBorderRatio).apply();
    }

    public Float getCusFrameVerticalBorderRatio() {
        if (cusFrameVerticalBorderRatio == null) {
            cusFrameVerticalBorderRatio = spf.getFloat("cusFrameVerticalBorderRatio", 0.f);
        }
        return cusFrameVerticalBorderRatio;
    }

    public void setCusFrameVerticalBorderRatio(Float cusFrameVerticalBorderRatio) {
        this.cusFrameVerticalBorderRatio = cusFrameVerticalBorderRatio;
        editor.putFloat("cusFrameVerticalBorderRatio", cusFrameVerticalBorderRatio).apply();
    }

    public void setWhiteBalance(float[] whiteBalance) {
        this.whiteBalance = whiteBalance;
        editor.putFloat("white_balance_r", whiteBalance[0]);
        editor.putFloat("white_balance_g", whiteBalance[1]);
        editor.putFloat("white_balance_b", whiteBalance[2]);
    }

    public float[] getWhiteBalance() {
        if (whiteBalance == null) {
            float r = spf.getFloat("white_balance_r", -1);
            if (r == -1) {
                whiteBalance = new float[] { 1, 1, 1, 1 };
            } else {
                float g = spf.getFloat("white_balance_g", 1);
                float b = spf.getFloat("white_balance_b", 1);
                whiteBalance = new float[] { r, g, b, 1 };
            }
        }
        return whiteBalance;
    }
}
